module nade.list;

import nade.array;


class List {
	this(Object head, Object[] rest ...)
	{
		_head = head;
		_tail = (rest.length == 0) ? null : new List(.head(rest), .tail(rest));
	}

	int opApply(int delegate(ref Object) dg)
	{
		const r = dg(_head);
		if(r != 0) return r;
		if(_tail !is null) return _tail.opApply(dg);
		return 0;
	}

	Object opIndex(uint i)
	{
		if(i > 0) return (_tail is null) ? null : _tail[i - 1];
		return _head;
	}

	Object head() { return _head; }
	Object head(List rhs) { return _head = rhs; }

	List tail() { return _tail; }
	List tail(List rhs) { return _tail = rhs; }

	private {
		Object		_head;
		List		_tail;
	}
}
